#include "sort_tiling.h"
#include "register/op_def_registry.h"
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
  SortTilingData tiling;
    auto input_shape = context->GetInputShape(0)->GetStorageShape();
    int axis = *context->GetAttrs()->GetInt(0);
    if(axis < 0) axis += input_shape.GetDimNum();
    bool descending = *context->GetAttrs()->GetBool(1);
    bool stable = *context->GetAttrs()->GetBool(2);
    int32_t repeat = 1;
    int32_t cycles = 1;
    int32_t interval = 1;
    for(int i=0; i<axis; i++)
    {
        repeat *= input_shape.GetDim(i);
    }
    cycles = input_shape.GetDim(axis);
    for(int i=axis+1; i<input_shape.GetDimNum(); i++)
    {
        interval *= input_shape.GetDim(i);
    }
    if(interval == 1)
    {
        if(descending == false)
        {
            context->SetTilingKey(1);
        }
        else
        {
            context->SetTilingKey(2);
        }
    }
    else
    {
        uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType()); 
        int32_t tileDataLenght = 32/inputBytes;
        int32_t loopCount = interval/tileDataLenght;
        int32_t tailDataLenght = interval - tileDataLenght*loopCount;
        if(tailDataLenght == 0)
        {
            tailDataLenght = tileDataLenght; 
            loopCount--;
        }
        tiling.set_loopCount(loopCount);
        tiling.set_tileDataLenght(tileDataLenght);
        tiling.set_tailDataLenght(tailDataLenght);
        if(descending == false)
        {
            context->SetTilingKey(3);
        }
        else
        {
            context->SetTilingKey(4);
        }
    }
    tiling.set_repeat(repeat);
    tiling.set_cycles(cycles);
    tiling.set_interval(interval);
  context->SetBlockDim(1);
  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  return ge::GRAPH_SUCCESS;
}
}
namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
static ge::graphStatus InferDataType(gert::InferDataTypeContext *context)
{
const auto inputDataType = context->GetInputDataType(0);
context->SetOutputDataType(0, inputDataType);
return ge::GRAPH_SUCCESS;
}
}
namespace ops {
class Sort : public OpDef {
public:
    explicit Sort(const char* name) : OpDef(name)
    {
        this->Input("input")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16, ge::DT_INT8, ge::DT_UINT8, ge::DT_INT32, ge::DT_INT64})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_BF16, ge::DT_INT8, ge::DT_UINT8, ge::DT_INT32, ge::DT_INT64})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Attr("axis").AttrType(OPTIONAL).Int(-1);
        this->Attr("descending").AttrType(OPTIONAL).Bool(false);
        this->Attr("stable").AttrType(OPTIONAL).Bool(false);
        this->SetInferShape(ge::InferShape).SetInferDataType(ge::InferDataType);
        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");
    }
};
OP_ADD(Sort);
}
